
var VMList = new Pearlsen.BaseEdit ({
	gridId:'VMGrid',
	formId:'VMGrid',
	advSearchId:'advSearchWin',
	module:'虚拟机维护',
	modulePath:'/pages/rms/VMMgr/',
	moduleName:'VMMgr',
	idProperty:'VM_ID',
	nameProperty:'VM_NAME',
	dataSetHandler:'VMMgrDatasetHandler',
	queryHandler:'VMMgrQueryHandler',
	initFun:function(){
		var VApp_Name = this.getQueryString("VApp_Name");
		var VDC_Name = this.getQueryString("VDC_Name");
		var query=new Query('VMMgrQueryHandler','VMMgrQueryHandler');
		query.setHandlerMethod("handler");
		query.addCondition("VApp_Name",VApp_Name);
		query.addCondition("VDC_Name",VDC_Name);
     	query.setCompletedFun(this.afterloadForm);
     	query.setCompletedFunScope(this);
     	query.load();		
	},

	afterloadForm:function(success,dataset,response, opts){
		if(success){
			if( dataset && !Ext.isEmpty(dataset.record)){
				this.dataset=dataset;			
				//VMMgrQueryHandler.add(dataset.record);
			}
		}else{
			
		}
	},
	
	statusRender:function(value){
        var val = value;
        switch (value) {
            case "POWERED_ON":
                val = "<span style='color:green;'>开机</span>";
                break;
            case "POWERED_OFF":
                val = "<span style='color:red;'>关机</span>";
                break;
            case "SUSPENDED":
                val = "<span style='color:yellow;'>挂起</span>";
                break;
            case "SHUTDOWN":
                val = "<span style='color:red;'>关机</span>";
                break;
            case "REBOOT":
                val = "<span style='color:red;'>重启</span>";
                break;
        }
        return val;
	},
	
	createVM:function(){

	},
	
	createVMCallBack:function(result)
	{
		if( result.success ){
			Pearlsen.Utils.showInfoMsg("创建虚拟机成功!");	
		}else{
			Pearlsen.Utils.showErrorMsg("出错了,原因:"+result.message);
		}
	},
	
	editVM:function(){

	},
	editVMCallBack:function(result)
	{
		if( result.success ){
			Pearlsen.Utils.showInfoMsg("编辑虚拟机成功!");	
		}else{
			Pearlsen.Utils.showErrorMsg("出错了,原因:"+result.message);
		}
	},
	
	deleteVM:function(){
		var me = VMList;
		var record= Ext.getCmp("VMGrid").getSelectionModel().getLastSelected();
		if(!record){
			Pearlsen.Utils.showInfoMsg("请选择一条记录");	
			return;
		}
		var ds=new DataSet('VMMgrDatasetHandler','VMMgrDatasetHandler');
      	ds.setHandlerMethod("delete");
      	ds.addCondition("VDC_Name",record.get('VDC_Name'));
      	ds.addCondition("VApp_Name",record.get('VApp_Name'));
      	ds.addCondition("VM_Name",record.get('VM_Name'));
		ds.execute(me.deleteVMCallBack);
	},
	
	deleteVMCallBack:function(result)
	{
		if( result.success ){
			Pearlsen.Utils.showInfoMsg("删除虚拟机成功!");	
		}else{
			Pearlsen.Utils.showErrorMsg("出错了,原因:"+result.message);
		}
	},
	
	powerOnVM:function()
	{
		var me = VMList;
		var record= Ext.getCmp("VMGrid").getSelectionModel().getLastSelected();
		if(!record){
			Pearlsen.Utils.showInfoMsg("请选择一条记录");	
			return;
		}
		var ds=new DataSet('VMMgrDatasetHandler','VMMgrDatasetHandler');
      	ds.setHandlerMethod("powerOnVM");
      	ds.addCondition("VM_Name",record.get('VM_Name'));
      	ds.addCondition("VDC_Name",record.get('VDC_Name'));
      	ds.addCondition("VApp_Name",record.get('VApp_Name'));
		ds.execute(me.powerOnVMCallBack);
	},
	
	powerOnVMCallBack:function(result)
	{
		if( result.success ){
			Pearlsen.Utils.showInfoMsg("正在开机!");	
			
		}else{
			Pearlsen.Utils.showErrorMsg("出错了,原因:"+result.message);
		}
	},
	
	shutdownVM:function()
	{
		var me = VMList;
		var record= Ext.getCmp("VMGrid").getSelectionModel().getLastSelected();
		if(!record){
			Pearlsen.Utils.showInfoMsg("请选择一条记录");	
			return;
		}
		var ds=new DataSet('VMMgrDatasetHandler','VMMgrDatasetHandler');
      	ds.setHandlerMethod("shutdownVM");
      	ds.addCondition("VM_Name",record.get('VM_Name'));
      	ds.addCondition("VDC_Name",record.get('VDC_Name'));
      	ds.addCondition("VApp_Name",record.get('VApp_Name'));
		ds.execute(me.shutdownVMCallBack);
	},
	
	shutdownVMCallBack:function(result)
	{
		if( result.success ){
			Pearlsen.Utils.showInfoMsg("正在关机!");		
		}else{
			Pearlsen.Utils.showErrorMsg("出错了,原因:"+result.message);
		}
	},
	
	suspendVM:function(){
		var me = VMList;
		var record= Ext.getCmp("VMGrid").getSelectionModel().getLastSelected();
		if(!record){
			Pearlsen.Utils.showInfoMsg("请选择一条记录");	
			return;
		}
		var ds=new DataSet('VMMgrDatasetHandler','VMMgrDatasetHandler');
      	ds.setHandlerMethod("suspendVM");
      	ds.addCondition("VM_Name",record.get('VM_Name'));
      	ds.addCondition("VDC_Name",record.get('VDC_Name'));
      	ds.addCondition("VApp_Name",record.get('VApp_Name'));
		ds.execute(me.suspendVMCallBack);
	},
	
	suspendVMCallBack:function(result)
	{
		if( result.success ){
			Pearlsen.Utils.showInfoMsg("正在挂起!");		
		}else{
			Pearlsen.Utils.showErrorMsg("出错了,原因:"+result.message);
		}
	},
	
	rebootVM:function(){
		var me = VMList;
		var record= Ext.getCmp("VMGrid").getSelectionModel().getLastSelected();
		if(!record){
			Pearlsen.Utils.showInfoMsg("请选择一条记录");	
			return;
		}
		var ds=new DataSet('VMMgrDatasetHandler','VMMgrDatasetHandler');
      	ds.setHandlerMethod("rebootVM");
      	ds.addCondition("VM_Name",record.get('VM_Name'));
      	ds.addCondition("VDC_Name",record.get('VDC_Name'));
      	ds.addCondition("VApp_Name",record.get('VApp_Name'));
		ds.execute(me.rebootVMCallBack);
	},
	
	rebootVMCallBack:function(result)
	{
		if( result.success ){
			Pearlsen.Utils.showInfoMsg("正在重启!");		
		}else{
			Pearlsen.Utils.showErrorMsg("出错了,原因:"+result.message);
		}
	},
	/**
	 * 填充主机参数
	 */
	fillHostForm:function(view,record,item,index,e,eOpts){
		var VM_Cpu = record.data['VM_Cpu'];
		var VM_Memory = record.data['VM_Memory'];
		var VM_HardDisk = record.data['VM_HardDisk'];
		var VM_Netcards = record.data['VM_Netcards']; 
		var memory =  parseInt( VM_Memory.substring(0,VM_Memory.length-3))/1024;
		var hardDisk = parseInt( VM_HardDisk.substring(0,VM_HardDisk.length-3) )/1024;
		var hostForm = Ext.getCmp("hostForm");
		hostForm.queryById("cups").setValue(VM_Cpu);
		hostForm.queryById("memory").setValue(memory);
		hostForm.queryById("storage").setValue(hardDisk);		
		Ext.getCmp("fmemoryunit").setValue('1'); 
		Ext.getCmp("fstorageunit").setValue('1'); 
	},
	/***
	 * 打开编辑主机参数Window
	 */
	editVMParam:function(){
		//modifyHostParams closeHostWin
		var record = Ext.getCmp("VMGrid").getSelectionModel().getLastSelected();
		if(!record){
			Pearlsen.Utils.showInfoMsg("请选择一条记录");	
			return;
		}
		var hostConfigWin = Ext.getCmp("hostConfigWin");
		hostConfigWin.setTitle("修改主机参数");
		hostConfigWin.show();
	},
	/***
	 * 确定修改主机参数
	 */
	modifyHostParams:function(){
		var me = VMList;
		var hasChange = false;
		var record = Ext.getCmp("VMGrid").getSelectionModel().getLastSelected();
		var oldCpu = parseInt(record.data['VM_Cpu']);
		var VM_Memory = record.data['VM_Memory'];
		var oldMemory = parseInt( VM_Memory.substring(0,VM_Memory.length-3));
		
		var VM_HardDisk = record.data['VM_HardDisk'];
		var oldHardDisk = parseInt( VM_HardDisk.substring(0,VM_HardDisk.length-3) );
		
		var munit = Ext.getCmp("fmemoryunit").getValue();
		var hunit=Ext.getCmp("fstorageunit").getValue(); 

		var newmemory=Ext.getCmp("memory").getValue(); 
		var newHardDisk=Ext.getCmp("storage").getValue(); 
		if( munit==1 ){
			newmemory = newmemory *1024;
		}
		if( hunit==1 ){
			newHardDisk = newHardDisk *1024;
		}else if( hunit==2){
			newHardDisk = newHardDisk *1024 * 1024;
		}
		if( newHardDisk < oldHardDisk ){
			Pearlsen.Utils.showInfoMsg("修改后的硬盘不能小于原硬盘大小");	
			return ;
		}
		var cpu= Ext.getCmp("cups").getValue(); 
		if( cpu != oldCpu){
			hasChange = true;
		}
		if( newmemory != oldMemory ){
			hasChange = true;
		}		
		if( newHardDisk != oldHardDisk ){
			hasChange = true;
		}
		if(!hasChange){
			me.closeHostWin();
			return ;
		}
		if(hasChange){
		       Ext.MessageBox.confirm('提示', '确定要更新主机【'+record.data['VM_Name']+'】的参数吗?',
		           	function(btn){
		   	        	if(btn=='yes'){
		   	        		Ext.Msg.wait("正在执行中,请稍后 ...",'',null);
		   	            	var dataSet = new DataSet("ResTaskDataSetHandler","_save_dataset");
		   	            	dataSet.setHandlerMethod("updateHostParam");
		   	            	dataSet.newRecord();
		   	            	dataSet.setValue("vdcName",record.data['VDC_Name']+"");
		   	            	dataSet.setValue("vappName",record.data['VApp_Name']+"");
		   	            	dataSet.setValue("vmName",record.data['VM_Name']+"");
		   	            	dataSet.setValue("cpu",cpu+"");
		   	            	dataSet.setValue("memory",newmemory+"");
		   	            	dataSet.setValue("appendDiskHard",(newHardDisk-oldHardDisk)+"");
		   	            	dataSet.execute(function(result){
		   	            		
		   	            		if(result.getStatus()=="200"){
		   	            			Pearlsen.Utils.showSuccessMsg('修改完成');
		   	            			Ext.getCmp("VMGrid").getStore().load();
		   	            			me.closeHostWin();
		   	            		}else{
		   	            			Pearlsen.Utils.showErrorMsg('修改失败,原因:'+result.message);
		   	            		}
		   	            		Ext.Msg.hide();
		   	            	});
		   	        	}
		           	}
		         );
		}
		

	},

	closeHostWin:function(){
		var hostConfigWin = Ext.getCmp("hostConfigWin");
		hostConfigWin.hide();
	}
});

